from fastapi import UploadFile

from app.common.constant import Constant
from app.common.error_code import ErrorCode
from app.common.result import Result
from app.utils import file_utils


def check_file_requirements(file: UploadFile) -> Result:
    """
    校验文件是否符合要求
    :param file: 用户上传的文件
    :return: Result
    """
    # 0.检查文件大小
    file_size = file.size
    if file_size > Constant.RAG_SUPPORT_FILE_SIZE:
        return Result.fail_with_error_code(ErrorCode.FILE_SIZE_EXCEEDED)

    # 1.检查文件类型是否支持
    file_suffix = file_utils.get_file_extension_name(file.filename)
    if file_suffix.upper() not in Constant.RAG_SUPPORT_FILE_SUFFIX:
        return Result.fail_with_error_code(ErrorCode.INVALID_FILE_SUFFIX)
    return Result.ok()
